#!/usr/bin/python
import pygst
pygst.require("0.10")
import gst
import gobject

class Playback:
    def __init__(self, filename):
        self.pipeline = gst.Pipeline('player')

        # Get the message bus from the pipeline
        self.bus = self.pipeline.get_bus()
        # register a watch function - self.message
        self.bus.add_watch(self.message)

        # create a filesrc->mad->alsasink pipeline
        filesrc = gst.element_factory_make("filesrc", "input")
        filesrc.set_property('location', filename)
        converter = gst.element_factory_make("mad", "converter")
        output = gst.element_factory_make("alsasink", "output")
        output.set_property('device', 'plughw:0')
        # Add them all to the pipeline with one call.
        self.pipeline.add(filesrc, converter, output)
        # Link them all together in one call.
        gst.element_link_many(filesrc, converter, output)
        self.pipeline.set_state(gst.STATE_PLAYING)

    def message(self, bus, message):
        # Check for an 'End Of Stream' message
        if message.type == gst.MESSAGE_EOS:
            print "Got EOS, quitting mainloop"
            self.mainloop.quit()
        # We want to continue receiving messages, so return True
        # If you return False, this function is removed from the list
        # of functions that receive bus messages.
        return True

    def run(self):
        self.mainloop = gobject.MainLoop()
        self.mainloop.run()
    def quit(self, *args):
        self.mainloop.quit()

if __name__ == "__main__":
    import sys
    player=Playback(sys.argv[1])
    player.run()
